package com.dementors.demo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dementors.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Author  : itxuye(itxuye@gmail.com)|(http://itxuye.com)
 * Date    : 2015-12-01
 * Time    : 00:12
 * FIXME
 */
public class RecycleListAdapter//demo
    extends RecyclerView.Adapter<RecycleListAdapter.HeadLineViewHolder> {

  private List<RecycleBean.DataEntity> mList;

    private Context mContext;

  public RecycleListAdapter(Context mContext) {
    this.mContext = mContext;
    mList = new ArrayList<>();
  }

  public void updateWithClear(List<RecycleBean.DataEntity> list) {
    mList.clear();
    mList.addAll(list);
    notifyDataSetChanged();
  }

  public void update(List<RecycleBean.DataEntity> list) {

    mList.addAll(list);
    notifyDataSetChanged();
  }

  public void updateStart(List<RecycleBean.DataEntity> list) {

    mList.addAll(0, list);
    notifyDataSetChanged();
  }

  public RecycleBean.DataEntity getData(int postion) {

    return mList.get(postion);
  }

  @Override
  public RecycleListAdapter.HeadLineViewHolder onCreateViewHolder(ViewGroup parent,
      int viewType) {

    View view = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.item_list_recycle, parent, false);

    view.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {

      }
    });

    return new HeadLineViewHolder(view);
  }

  @Override
  public void onBindViewHolder(final RecycleListAdapter.HeadLineViewHolder holder,
      int position) {

    final RecycleBean.DataEntity dataEntity = mList.get(position);

    if (dataEntity.getWapThumb().equals("")) {
      Picasso.with(mContext)
          .load(R.mipmap.ic_launcher)
          .resize(180, 180)
          .into(holder.imageview_wap_thumb);
    } else {
      Picasso.with(mContext)
          .load(dataEntity.getWapThumb())
          .resize(180, 180)
          .placeholder(R.mipmap.ic_launcher)
          .into(holder.imageview_wap_thumb);
    }

    holder.text_title.setText(dataEntity.getTitle());
    holder.text_source_create_nickname_time.setText(dataEntity.getSource()
        + " "
        + dataEntity.getNickname()
        + " "
        + dataEntity.getCreateTime());
  }

  @Override public int getItemCount() {
    return mList.size();
  }

  static class HeadLineViewHolder extends RecyclerView.ViewHolder {

    //@formatter:off
    @Bind(R.id.text_title)
    TextView text_title;
    @Bind(R.id.text_source_create_nickname_time)
    TextView text_source_create_nickname_time;
    @Bind(R.id.imageview_wap_thumb)
    ImageView imageview_wap_thumb;
    //@formatter:on

    public HeadLineViewHolder(View view) {
      super(view);
      ButterKnife.bind(this, view);
    }
  }
}



